// pages/game/game.js
import regeneratorRuntime from '../../utils/regenerator-runtime/runtime-module.js'
import api from '../../utils/api.js'
const { $Toast } = require('../../iview/base/index')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    game: 'game',
    leaderboard: {},
    integral: 0,
    name: '',
    score: 0,
    image_1: '/img/shaizi_1.png',
    image_2: '/img/shaizi_2.png',
    image_3: '/img/shaizi_3.png',
    image_4: '/img/shaizi_4.png',
    image_5: '/img/shaizi_5.png',
    image_6: '/img/shaizi_6.png',
    move_1: '',
    move_2: '',
    move_3: '',
    move_4: '',
    move_5: '',
    move_6: '',
    visable_1: false,
    actions_1: [
      {
        name: '确定'
      }
    ],
    maskShow: true
  },

  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: async function (options) {
    wx.showLoading({
      title: '加载中'
    })
    this.onComponentsShow()
    if (!await api.checkLogin()){
      let authResponse = await api.login()
    }
    let leaderboardData = await api.authRequest("/user?include=rank")
    let shareId = wx.getStorageSync('shareId')
    if (shareId) {
      // TO 分享得次数
      let shareResponse = await api.authRequest({
        url: "/moonCakeCounts?include=rank",
        data: { target_id: shareId},
        method: "POST"
      })
      wx.removeStorageSync('shareId')
    }
    
    this.setData({
      leaderboard: leaderboardData.data
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    setTimeout( () => {
      this.setData({
        maskShow: false
      })
      wx.hideLoading()
    }, 1000)
 },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    wx.showShareMenu({
      withShareTicket: true
    })
    return {
      title: '戳这里，各款美酒任性纵饮！',
      path: "/pages/index/index?id=" + this.data.leaderboard.id,
      success: (res) => {
        console.log('成功', res)
      },
      fail: (res) => {
        console.log("转发失败", res)
      }
    }
  },

  onComponentsShow() {
    this.bottomNavigation = this.selectComponent("#game")
    this.bottomNavigation._displayShow()
  },

  async gameStart () {
    if (this.data.leaderboard.moon_cake_count === 0) {
      this.setData({
        visable_1: true
      })
      return false;
    }
    this.setData({
      move_1: 'image-1',
      move_2: 'image-2',
      move_3: 'image-3',
      move_4: 'image-4',
      move_5: 'image-5',
      move_6: 'image-6'
    })
    let resphone = await api.authRequest({
      method: "POST",
      url: "/moonCakeBetting?include=user.rank",
      data: {

      }
    })
    if(resphone.statusCode === 200) {
      this.setData({
        name: resphone.data.name,
        score: resphone.data.score,
        image_1: `/img/shaizi_${resphone.data.game_res[0]}.png`,
        image_2: "/img/shaizi_" + resphone.data.game_res[1] +".png",
        image_3: "/img/shaizi_" + resphone.data.game_res[2] +".png",
        image_4: "/img/shaizi_" + resphone.data.game_res[3] +".png",
        image_5: "/img/shaizi_" + resphone.data.game_res[4] +".png",
        image_6: "/img/shaizi_" + resphone.data.game_res[5] +".png",
      })
      setTimeout(() => {
        this.setData({
          move_1: '',
          move_2: '',
          move_3: '',
          move_4: '',
          move_5: '',
          move_6: '',
          leaderboard: resphone.data.user
        })
        setTimeout( () => {
          $Toast({
            content: this.data.name + '(' + [this.data.score] + ')' + "分"
          })
        }, 100)
      }, 1000)
        
    } else if (resphone.statusCode === 400 || resphone.statusCode === 422) {
      $Toast({
        content: resphone.data.message
      }) 
    } else {
      $Toast({
        content: '服务器错误，请重试！'
      }) 
    }
  },

  focusClick_1 ({ detail }) {
    this.setData({
      visable_1: false
    })
  }
})